package com.server;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;

public class EsServiceImpl {
    private static final Logger logger = LoggerFactory.getLogger(EsServiceImpl.class);
    private ElasticsearchTemplate elasticsearchTemplate;


    private volatile long count = 0;
    private volatile long fails = 0;
    private volatile long succeeds = 0;

    public void pubData(String nodeJson,String type){
        try {
            IndexQuery query = new IndexQueryBuilder()
                    .withIndexName("bit_apm").withType(type)
                    .withSource(nodeJson).build();
            elasticsearchTemplate.index(query);
            succeeds++;
        }catch (Exception e){
            logger.error("上传es失败",e);
            fails++;
        }finally {
            count++;
        }

    }

    public String getStatistic(){
        return "总数：" +count +"成功数：" + succeeds+
                "失败数：" + fails;
    }
}
